﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZR.DataCollection.SingleParameter
{
    public class ProductInfoManager
    {
        private static readonly Lazy<ProductInfoManager> _instance =
        new Lazy<ProductInfoManager>(() => new ProductInfoManager());

        public static ProductInfoManager Instance => _instance.Value;

        #region 变量

        private readonly ConcurrentDictionary<string, string> _currentProductType = new();
        private readonly ConcurrentDictionary<string, string> _currentProductNo = new();
        private readonly ConcurrentDictionary<string, string> _currentOrderCode = new();
        private ConcurrentDictionary<string, DateTime> _currentStartTime = new();

        #endregion 变量

        private ProductInfoManager()
        { }

        public void SetProductType(string key, string value) => _currentProductType[key] = value;

        public string GetProductType(string key) => _currentProductType.GetValueOrDefault(key);

        public void SetProductNo(string key, string value) => _currentProductNo[key] = value;

        public string GetProductNo(string key) => _currentProductNo.GetValueOrDefault(key);

        public void SetOrderCode(string key, string value) => _currentOrderCode[key] = value;

        public string GetOrderCode(string key) => _currentOrderCode.GetValueOrDefault(key);

        public void SetStartTime(string key, DateTime value) => _currentStartTime[key] = value;

        public DateTime GetStartTime(string key) => _currentStartTime.GetValueOrDefault(key);
    }

    // 扩展方法
    public static class Extensions
    {
        public static TValue GetValueOrDefault<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dict, TKey key)
        {
            dict.TryGetValue(key, out var value);
            return value;
        }
    }
}